package com.yuncheng.oaApi.controller;

import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.entity.NkRsXmbxmbdl;
import com.yuncheng.oaApi.entity.NkRsXmbxmbxl;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.service.INkRsXmbxmbdlService;
import com.yuncheng.oaApi.service.INkRsXmbxmbxlService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.NkRsBxmxxm;
import com.yuncheng.oaApi.service.INkRsBxmxxmService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 内控-报销明细项目
 * @Author: lhb
 * @Date: 2023-11-24
 * @Version: V1.0
 */
@Api(tags = "NK_内控-报销明细项目接口")
@RestController
@RequestMapping("/api/spzx-oa/nkRsBxmxxm")
public class NkRsBxmxxmController extends BaseController<NkRsBxmxxm, INkRsBxmxxmService> {

    private static final Logger log = LoggerFactory.getLogger(NkRsBxmxxmController.class);

    @Resource
    private INkRsBxmxxmService nkRsBxmxxmService;

    @Resource
    @Lazy
    private INkRsXmbxmbdlService nkRsXmbxmbdlService;

    @Resource
    @Lazy
    private INkRsXmbxmbxlService nkRsXmbxmbxlService;


    /**
     * 分页列表查询
     *
     * @param nkRsBxmxxm
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "内控-报销明细项目-分页列表查询")
    @ApiOperation(value = "内控-报销明细项目-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(NkRsBxmxxm nkRsBxmxxm, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<NkRsBxmxxm> queryWrapper = QueryBuilder.initQueryWrapper(nkRsBxmxxm, req.getParameterMap());
        Page<NkRsBxmxxm> page = new Page<>(pageNo, pageSize);
        IPage<NkRsBxmxxm> pageList = nkRsBxmxxmService.page(page, queryWrapper);
        for(NkRsBxmxxm vo:pageList.getRecords()){
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }


    /**
     * 查询报销大类模板
     */
    @AutoLog(value = "查询报销大类模板")
    @ApiOperation(value = "查询报销大类模板")
    @GetMapping(value = "/selectdllist")
    public HttpResult<?> selectdllist(){
        List<NkRsXmbxmbdl> list = nkRsXmbxmbdlService.list(new LambdaQueryWrapper<NkRsXmbxmbdl>());
        return HttpResult.ok(list);

    }

    /**
     * 查询报销小类模板
     */
    @AutoLog(value = "查询报销小类模板-根据大类id")
    @ApiOperation(value = "查询报销小类模板-根据大类id")
    @GetMapping(value = "/selectxllist")
    public HttpResult<?> selectxllist(@RequestParam(value = "dlid",defaultValue = "") String dlid){
        if (StringUtils.isBlank(dlid)){
            throw new BusinessException("数据参数为空!");
        }

        List<NkRsXmbxmbxl> list = nkRsXmbxmbxlService.list(new LambdaQueryWrapper<NkRsXmbxmbxl>()
                .eq(NkRsXmbxmbxl::getFxmbxmbdlid,dlid));

        return HttpResult.ok(list);

    }

    @AutoLog(value = "内控-报销明细项目-添加")
    @ApiOperation(value = "内控-报销明细项目-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody NkRsBxmxxm nkRsBxmxxm) {
        HttpResult<String> result = new HttpResult(true);
        nkRsBxmxxmService.save(nkRsBxmxxm);
        result.setResult(nkRsBxmxxm.getId());
        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }

    @AutoLog(value = "内控-报销明细项目-编辑")
    @ApiOperation(value = "内控-报销明细项目-编辑")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody NkRsBxmxxm nkRsBxmxxm) {
        nkRsBxmxxmService.updateById(nkRsBxmxxm);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    @AutoLog(value = "内控-报销明细项目-通过id删除")
    @ApiOperation(value = "内控-报销明细项目-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        nkRsBxmxxmService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    @AutoLog(value = "内控-报销明细项目-批量删除")
    @ApiOperation(value = "内控-报销明细项目-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        nkRsBxmxxmService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    @AutoLog(value = "内控-报销明细项目-通过id查询")
    @ApiOperation(value = "内控-报销明细项目-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        NkRsBxmxxm nkRsBxmxxm = nkRsBxmxxmService.getById(id);
        return HttpResult.ok(nkRsBxmxxm);
    }

    /**
     * 数据转换
     * @param nkRsBxmxxm
     */
    private void formatValue(NkRsBxmxxm nkRsBxmxxm){
    }
}
